const mongoose = require('mongoose');
const Product = require('../src/models/product.model');
const Category = require('../src/models/category.model');
require('./db'); // 连接数据库

// 产品种子数据 - 使用有效的图片链接
const seedProducts = [
  {
    name: "小米13 Pro 5G手机",
    description: "小米13 Pro搭载第二代骁龙8移动平台，徕卡专业光学镜头，1英寸大底主摄，支持75W有线快充。",
    price: 4999,
    originalPrice: 5999,
    mainImage: "https://images.unsplash.com/photo-1511707171634-5f897ff02aa9?w=400&h=400&fit=crop",
    images: [
      "https://images.unsplash.com/photo-1511707171634-5f897ff02aa9?w=400&h=400&fit=crop",
      "https://images.unsplash.com/photo-1565849904461-04a58ad377e0?w=400&h=400&fit=crop"
    ],
    brand: "小米",
    category: "手机数码",
    subcategory: "智能手机",
    tags: ["5G", "徕卡", "快充", "旗舰"],
    countInStock: 100,
    rating: 4.8,
    numReviews: 1250,
    isFeatured: true,
    isOnSale: true,
    salesCount: 3200,
    specifications: {
      "屏幕尺寸": "6.73英寸",
      "处理器": "骁龙8 Gen2",
      "内存": "12GB",
      "存储": "256GB",
      "电池": "4820mAh"
    }
  },
  {
    name: "华为Mate 50 Pro",
    description: "华为Mate 50 Pro，搭载麒麟9000S芯片，超光变XMAGE影像，支持66W超级快充。",
    price: 6799,
    originalPrice: 7999,
    mainImage: "https://images.unsplash.com/photo-1574944985070-8f3ebc6b79d2?w=400&h=400&fit=crop",
    images: [
      "https://images.unsplash.com/photo-1574944985070-8f3ebc6b79d2?w=400&h=400&fit=crop",
      "https://images.unsplash.com/photo-1556656793-08538906a9f8?w=400&h=400&fit=crop"
    ],
    brand: "华为",
    category: "手机数码",
    subcategory: "智能手机",
    tags: ["XMAGE", "快充", "旗舰", "影像"],
    countInStock: 80,
    rating: 4.7,
    numReviews: 980,
    isFeatured: true,
    isOnSale: false,
    salesCount: 2100
  },
  {
    name: "iPhone 15 Pro Max",
    description: "iPhone 15 Pro Max，搭载A17 Pro芯片，钛金属设计，专业级摄像系统，支持USB-C。",
    price: 9999,
    originalPrice: 10999,
    mainImage: "https://images.unsplash.com/photo-1510557880182-3d4d3cba35a5?w=400&h=400&fit=crop",
    images: [
      "https://images.unsplash.com/photo-1510557880182-3d4d3cba35a5?w=400&h=400&fit=crop",
      "https://images.unsplash.com/photo-1592750475338-74b7b21085ab?w=400&h=400&fit=crop"
    ],
    brand: "苹果",
    category: "手机数码",
    subcategory: "智能手机",
    tags: ["A17 Pro", "钛金属", "专业摄影", "USB-C"],
    countInStock: 50,
    rating: 4.9,
    numReviews: 2100,
    isFeatured: true,
    isOnSale: true,
    salesCount: 5600
  },
  {
    name: "小米笔记本Pro 15",
    description: "小米笔记本Pro 15，搭载第12代英特尔酷睿处理器，15.6英寸3.5K OLED屏幕，轻薄便携。",
    price: 5999,
    originalPrice: 6999,
    mainImage: "https://picsum.photos/400/400?random=7",
    images: [
      "https://picsum.photos/400/400?random=7",
      "https://picsum.photos/400/400?random=8"
    ],
    brand: "小米",
    category: "电脑办公",
    subcategory: "笔记本电脑",
    tags: ["OLED", "轻薄", "高性能", "便携"],
    countInStock: 60,
    rating: 4.6,
    numReviews: 450,
    isFeatured: false,
    isOnSale: true,
    salesCount: 890
  },
  {
    name: "戴森V15 Detect无线吸尘器",
    description: "戴森V15 Detect，激光探测技术，智能感应灰尘，强劲吸力，长续航。",
    price: 4690,
    originalPrice: 5190,
    mainImage: "https://picsum.photos/400/400?random=9",
    images: [
      "https://picsum.photos/400/400?random=9",
      "https://picsum.photos/400/400?random=10"
    ],
    brand: "戴森",
    category: "家用电器",
    subcategory: "清洁电器",
    tags: ["无线", "激光探测", "强劲吸力", "长续航"],
    countInStock: 30,
    rating: 4.8,
    numReviews: 320,
    isFeatured: true,
    isOnSale: true,
    salesCount: 650
  },
  {
    name: "美的变频空调 1.5匹",
    description: "美的变频空调，一级能效，智能控制，静音运行，快速制冷制热。",
    price: 2899,
    originalPrice: 3599,
    mainImage: "https://picsum.photos/400/400?random=11",
    images: [
      "https://picsum.photos/400/400?random=11",
      "https://picsum.photos/400/400?random=12"
    ],
    brand: "美的",
    category: "家用电器",
    subcategory: "空调",
    tags: ["变频", "一级能效", "智能", "静音"],
    countInStock: 120,
    rating: 4.5,
    numReviews: 890,
    isFeatured: false,
    isOnSale: true,
    salesCount: 1200
  }
];

// 分类种子数据
const seedCategories = [
  {
    name: "手机数码",
    slug: "mobile-digital",
    description: "智能手机、平板电脑、数码配件等",
    icon: "phone-portrait-outline",
    isTopLevel: true,
    isActive: true,
    order: 1
  },
  {
    name: "电脑办公",
    slug: "computer-office",
    description: "笔记本电脑、台式机、办公设备等",
    icon: "laptop-outline",
    isTopLevel: true,
    isActive: true,
    order: 2
  },
  {
    name: "家用电器",
    slug: "home-appliances",
    description: "空调、冰箱、洗衣机、小家电等",
    icon: "home-outline",
    isTopLevel: true,
    isActive: true,
    order: 3
  },
  {
    name: "服装鞋靴",
    slug: "clothing-shoes",
    description: "男装、女装、童装、鞋靴等",
    icon: "shirt-outline",
    isTopLevel: true,
    isActive: true,
    order: 4
  },
  {
    name: "美妆护肤",
    slug: "beauty-skincare",
    description: "化妆品、护肤品、个人护理等",
    icon: "flower-outline",
    isTopLevel: true,
    isActive: true,
    order: 5
  }
];

// 清空并重新填充数据库
async function seedDatabase() {
  try {
    console.log('🌱 开始清空数据库...');

    // 清空现有数据
    await Product.deleteMany({});
    await Category.deleteMany({});

    console.log('✅ 数据库清空完成');

    console.log('🌱 开始添加分类数据...');
    const categories = await Category.insertMany(seedCategories);
    console.log(`✅ 成功添加 ${categories.length} 个分类`);

    console.log('🌱 开始添加产品数据...');
    const products = await Product.insertMany(seedProducts);
    console.log(`✅ 成功添加 ${products.length} 个产品`);

    console.log('🎉 种子数据添加完成！');

    // 显示添加的数据统计
    console.log('\n📊 数据统计:');
    console.log(`- 分类数量: ${categories.length}`);
    console.log(`- 产品数量: ${products.length}`);
    console.log(`- 精选产品: ${products.filter(p => p.isFeatured).length}`);
    console.log(`- 促销产品: ${products.filter(p => p.isOnSale).length}`);

  } catch (error) {
    console.error('❌ 种子数据添加失败:', error);
  } finally {
    // 关闭数据库连接
    mongoose.connection.close();
  }
}

// 运行种子数据脚本
if (require.main === module) {
  seedDatabase();
}

module.exports = { seedDatabase, seedProducts, seedCategories };
